package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;

import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;

import com.zzyl.vo.NursingProjectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    NursingPlanMapper nursingPlanMapper;

    @Autowired
    NursingProjectMapper nursingProjectMapper;

    @Autowired
    NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    NursingLevelMapper nursingLevelMapper;


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo selectById(Long id) {

        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);

        if (nursingPlan == null) {
            throw new BaseException(BasicEnum.NURSING_PLAN_NOT_EXIST_ERROR);
        }
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtil.copyProperties(nursingPlan, nursingPlanVo);

        List<NursingProjectPlan> nursingProjectPlan = nursingProjectPlanMapper.selectByPlanId(id);

        List<NursingProjectPlanVo> nursingProjectPlanVos =
                BeanUtil.copyToList(nursingProjectPlan, NursingProjectPlanVo.class);
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);

        return nursingPlanVo;
    }

    @Override
    public List<NursingPlanVo> getAllPlan() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.getAllPlan();
       /* List<NursingPlanVo> nursingPlanVos = nursingPlans.stream().map(
                plan -> {
                    NursingPlanVo nursingPlanVo = new NursingPlanVo();
                    BeanUtil.copyProperties(plan, nursingPlanVo);
                    return nursingPlanVo;
                }
        ).collect(Collectors.toList());*/
        List<NursingPlanVo> nursingPlanVos = nursingPlans.stream().map(new Function<NursingPlan, NursingPlanVo>() {
            @Override
            public NursingPlanVo apply(NursingPlan nursingPlan) {
                NursingPlanVo nursingPlanVo = new NursingPlanVo();
                BeanUtil.copyProperties(nursingPlan, nursingPlanVo);
                return nursingPlanVo;
            }
        }).collect(Collectors.toList());
        return nursingPlanVos;
    }


    /**
     * 新增
     *
     * @param nursingPlanDto
     */
    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {

        NursingPlan nursingPlanByName = nursingPlanMapper.selectByName(nursingPlanDto.getPlanName());
        if (nursingPlanByName != null) {
            throw new BaseException(BasicEnum.NURSING_PLAN_EXIST_ERROR);
        }
        Long proId = nursingProjectPlanMapper.selectRelativeProjectId(nursingPlanDto.getId());
        NursingProject nursingProject = nursingProjectMapper.selectById(proId);
        if (nursingProject != null) {
            throw new BaseException(BasicEnum.NURSING_EXIST_ERROR);
        }
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlanDto.setStatus(0);
        BeanUtil.copyProperties(nursingPlanDto, nursingPlan);
        if (nursingPlan == null) {
            throw new BaseException(BasicEnum.NURSING_NOT_EXIST_ERROR);
        }
        nursingPlanMapper.addNursingPlan(nursingPlan);

        List<NursingProjectPlanVo> projectPlans = nursingPlan.getProjectPlans();
        if (projectPlans == null) {
            throw new BaseException(BasicEnum.NURSING_RELATIVE_NOT_EXIST_ERROR);
        }
       /* projectPlans.forEach(p -> p.setPlanId(nursingPlan.getId()));*/
        /*for (NursingProjectPlanVo projectPlan : projectPlans) {
            projectPlan.setPlanId(nursingPlan.getId());
        }*/
        projectPlans.forEach(p ->
                p.setPlanId(nursingPlan.getId())
        );
        nursingProjectPlanMapper.addNursingPlanParam(projectPlans);

    }


    /**
     * 条件分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<NursingPlan>> page = nursingPlanMapper.selectByPage(name, status);
        return PageResponse.of(page, NursingPlanVo.class);
    }


    /**
     * 修改
     * @param nursingPlanDto
     */
    @Override
    public void modifyPlan(NursingPlanDto nursingPlanDto) {


        NursingPlan nursingPlan1 = nursingPlanMapper.selectById(nursingPlanDto.getId());
       /* List<NursingLevel> nursingLevel=nursingLevelMapper.selectByPlanId(nursingPlanDto.getId());
        if (nursingLevel!=null){
            throw new BaseException(BasicEnum.NURSING_PLAN_RELATIVE_LEVEL_ERROR);
        }*/
        NursingPlan nursingPlan = new NursingPlan();
        //主要逻辑
        BeanUtil.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.modifyPlan(nursingPlan);

        Long planId = nursingPlan.getId();
        nursingProjectPlanMapper.deleteById(planId);
        nursingPlan.getProjectPlans().forEach(plan -> {
            plan.setPlanId(nursingPlan.getId());
        });
       /* NursingPlan nursingPlanByName=nursingPlanMapper.selectByName(nursingPlanDto.getPlanName());
        if (nursingPlanByName!=null){
            throw new BaseException(BasicEnum.NURSING_PLAN_EXIST_ERROR);
        }*/
        nursingProjectPlanMapper.addNursingPlanParam(nursingPlan.getProjectPlans());
    }


    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //存在多条数据
      /*  NursingLevel nursingLevel=nursingLevelMapper.selectByPlanId(id);
        if (nursingLevel!=null){
            throw new BaseException(BasicEnum.NURSING_PLAN_RELATIVE_LEVEL_ERROR);
        }*/
        nursingProjectPlanMapper.deleteById(id);
        nursingPlanMapper.deleteById(id);

    }


    /**
     * 启用禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {

        // todo 存在多条数据
        /*NursingLevel nursingLevel=nursingLevelMapper.selectByPlanId(id);
        if (nursingLevel!=null){
            throw new BaseException(BasicEnum.NURSING_PLAN_RELATIVE_LEVEL_ERROR);
        }*/
        nursingPlanMapper.enableOrDisable(id, status);
    }

}
